import {Dispatch} from 'redux'
import {AnyAction} from 'redux'
import actionTypes from "../action-types"
import {serviceLogin, serviceLogout, serviceLoginByToken} from "../../request/user";
import {setToken} from "../../request/auth";
import {response} from "express";


const loginStart = () => {

    return {
        type: actionTypes.START_LOGIN,
        isLoading: false,
    }

};
const loginSuccess = (token: string, userInfo: object) => {
    return {
        type: actionTypes.LOGIN_SUCCESS,
        isLoading: false,
        payload: {userInfo},
        token: token,
    }
};
const loginFailed = () => {
    return {
        type: actionTypes.LOGIN_FAILED,
        isLoading: false,
    }
};

export function validateLocalStatus() {
    let userInfo = {};

    return '';
}

//登录
export const login = (data: object) => (dispatch: Dispatch) => {
    return new Promise((resolve, reject) => {
        dispatch(loginStart());
        serviceLogin(data).then((response: any) => {
            if (response.data.success) {
                //TODO  设置信息
                debugger;
                const token = response.data.data.token;
                const userInfo = response.data.data.userInfo;
                dispatch(loginSuccess(token, userInfo));
                console.log(token);
                console.log(userInfo);
                setToken(response.data.data.token);
                window.localStorage.setItem("authToken", token);
                window.localStorage.setItem("userInfo", JSON.stringify(response.data.data.userInfo));

                resolve(response);
            } else {
                dispatch(loginFailed());
                resolve(response);
            }

        }).catch((error) => {
            dispatch(loginFailed());
            reject(error);
        })

    })
};

//token登录
export function loginByToken(token: string) {

    return (dispatch: Dispatch) => {
        serviceLoginByToken(token).then(response => {
            dispatch(loginStart());
            if (response.data.success) {
                const token = response.data.data.token;
                const userInfo = response.data.data.userInfo;
                dispatch(loginSuccess(token, userInfo));
                console.log(token);
                console.log(userInfo);
                // debugger;
                setToken(response.data.data.token);
                window.localStorage.setItem("authToken", token);
                window.localStorage.setItem("userInfo", JSON.stringify(response.data.data.userInfo));

            } else {
                dispatch(loginFailed());
            }
            return response;
        }).catch((error) => {
            dispatch(loginFailed());
            return error;

        }).finally(() => {
            dispatch(loginFailed());
            return "";
        })
    }
}

//退出登录
/*退出*/
export const logout = (data: object) => {
    return (dispatch: Dispatch) => {
        dispatch(loginStart());
        // debugger;
        serviceLogout(data).then(response => {
            console.log(response);
            if (response.data.success) {
                dispatch(loginFailed())
            } else {
                dispatch(loginFailed())
            }
        });
    }
};

